package com.interviews.common;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 通用线程创建工厂
 *
 * @author qian
 * @version 1.0
 * @date 2022/3/8 17:25
 */
public class CommonThreadFactory implements ThreadFactory {
    private final String namePrefix;
    private final AtomicInteger nextId = new AtomicInteger(1);

    public CommonThreadFactory(String namePrefix) {
        this.namePrefix = "From CommonThreadFactory " + namePrefix + "-Worker-";
    }

    @Override
    public Thread newThread(Runnable r) {
        String name = namePrefix + nextId.getAndIncrement();
        Thread thread = new Thread(null, r, name, 0);
        System.out.println(thread.getName());
        return thread;
    }
}
